package com.ease.util {

   import mx.core.Application;
   import mx.core.FlexGlobals;
   import mx.utils.URLUtil;

   /**
    * Remote services utilities.
    */
   public class ServiceUtil {
      /**
       * The default service channel uri "messagebroker/amf"
       */
      private static const DEFAULT_CHANNEL_URI:String = "messagebroker/amf";

      /**
       * The default secure service channel uri "messagebroker/amfsecure"
       */
      private static const DEFAULT_SECURE_CHANNEL_URI:String = "messagebroker/amfsecure";

      /**
       * Returns the default channel uri for a given context path.
       * For example, given "app-container", it returns "/app-container/messagebroker/amf".
       *
       * @param contextPath
       *    Application's web context path.
       *
       * @param secure
       *    Determines if secured (i.e. SSL) channel needs to be used.
       *    By default set to null, in which case application url is used to determine
       *    what channel to use. i.e. If application url contains https,
       *    uses secured amf channel else uses the default amf channel.
       */
      public static function getDefaultChannelUri(
         contextPath:String,
         secure:Object=null):String {
         if (contextPath == null) {
            return null;
         }

         var val:Boolean = (secure != null) ?
            Boolean(secure) :
            RemoteService.defaultSecure;
         var channelUri:String = (val) ? DEFAULT_SECURE_CHANNEL_URI : DEFAULT_CHANNEL_URI;

         if (URLUtil.isHttpURL(contextPath)) {
            return URLUtil.getFullURL(contextPath, channelUri)
         } else {
            return "/" + contextPath + "/" + channelUri;
         }
      }
   }
}